//
//  ICDocumentViewController.m
//  ICome
//
//  Created by ENN on 16/3/29.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICDocumentViewController.h"
#import "ICFileTool.h"
#import "ICDocumentCell.h"
#import "ICEmptyStyleView.h"
#import "ICFileScanController.h"
#import "ICHeaderFile.h"

@interface ICDocumentViewController ()<UITableViewDataSource,UITableViewDelegate,ICDocumentCellDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIButton *rightBtn;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong)ICEmptyStyleView *emptyView;

@end

@implementation ICDocumentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    
    [self setupSubviews];
    
    [self loadData];

}



#pragma mark - NAV

- (void)setupNav
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"本机文件";
    
    if (!self.isFun) {
        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44., 44.)];
        [rightButton setTitle:@"发送" forState:UIControlStateNormal];
        rightButton.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [rightButton addTarget:self action:@selector(rightBarButtonClicked)forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
        [rightButton setTitleColor:ICRGB(0xDADADA) forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem.enabled = NO;
        self.rightBtn = rightButton;
    }
        
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImageName:@"App_back_blue" itemType:UIBarButtonItemTypeLeft target:self action:@selector(leftBarButtonClicked)];
}

#pragma mark - Event

- (void)rightBarButtonClicked
{
    if (!self.isFun) {
        
        if (!self.name) return;
        if ([self.delegate respondsToSelector:@selector(selectedFileName:)]) {
            [self.delegate selectedFileName:self.name];
        }
    }
     [self.navigationController popViewControllerAnimated:YES];
}

- (void)leftBarButtonClicked
{
    [self.navigationController popViewControllerAnimated:YES];
    //[self dismissViewControllerAnimated:YES completion:nil];
    if ([self.delegate respondsToSelector:@selector(cancelSelectedFile)]) {
        [self.delegate cancelSelectedFile];
    }
}

- (void)loadData
{
    if (!self.isFun) {
        NSArray *files = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:[ICFileTool fileMainPath] error:nil];
        [self.dataArr addObjectsFromArray:files];
    }
    if (self.dataArr.count == 0) {
        self.emptyView.hidden = NO;
    } else {
        self.emptyView.hidden = YES;
    }
}


#pragma mark - UI

- (void)setupSubviews
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height-64) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate   = self;
    [self.view addSubview:tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICDocumentCell *cell = [ICDocumentCell cellWithTableView:tableView];
    if (self.isFun) {
        
        cell.selectBtn.hidden = YES;
    }else
    {
        cell.delegate = self;
        cell.name = self.dataArr[indexPath.row];
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICFileScanController *scanVC = [[ICFileScanController alloc] init];
   
    
        ICDocumentCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        scanVC.filePath              = cell.filePath;
        scanVC.orgName               = [cell.name originName];
    [self.navigationController pushViewController:scanVC animated:YES];
}

#pragma mark - ICDocumentCellDelegate

- (void)selectBtnClicked:(id)sender
{
    UIButton *button = (UIButton *)sender;
    ICDocumentCell *cell = (ICDocumentCell *)[[button superview] superview];
    NSIndexPath *curIndexPath = [self.tableView indexPathForCell:cell];
    
    for (int row = 0; row < self.dataArr.count; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        if (curIndexPath != indexPath) {
            ICDocumentCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            cell.selectBtn.selected = NO;
        }
    }
    
    self.name = cell.name;
    button.selected = !button.selected;
    if (button.selected) {
        self.navigationItem.rightBarButtonItem.enabled = YES;
        [self.rightBtn setTitleColor:DEFAULT_SUBJECT_COLOR forState:UIControlStateNormal];
    }else {
        self.navigationItem.rightBarButtonItem.enabled = NO;
        [self.rightBtn setTitleColor:ICRGB(0xDADADA) forState:UIControlStateNormal];
    }
}

#pragma mark - Getter

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(ICEmptyStyleView *)emptyView {
    if (!_emptyView) {
        _emptyView = [[ICEmptyStyleView alloc] initWithTitle:@"暂无本地文件" placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_message_empty"] imageURL:@""];
        _emptyView.hidden = YES;
        _emptyView.frame = CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height);
        [self.view addSubview:_emptyView];
    }
    return _emptyView;
}


@end
